package com.lz.ovuola.manager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lz.ovuola.util.constant.AliDayuConsts;
import com.lz.ovuola.util.threadlocal.UserContextHolder;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumQueryResponse;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * 短信服务
 * 
 * @author fz
 *
 */
public abstract class AbstractAliDayuSmsApi {

	private static Logger logger = LoggerFactory
			.getLogger("AbstractAliDayuSmsApi");

	/**
	 * 发送短信
	 * 
	 * @param recNum
	 */
	public static final String send(String recNum, String code) {
		TaobaoClient client = new DefaultTaobaoClient(AliDayuAppInfo.getUrl(),
				AliDayuAppInfo.getAppkey(), AliDayuAppInfo.getSecret());
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setSmsType(AliDayuConsts.SMS_TYEP);
		req.setSmsFreeSignName(AliDayuConsts.SMS_FREE_SIGN_NAME);
		req.setSmsParamString("{\"number\":\"" + code + "\",\"product\":"
				+ AliDayuConsts.SMS_REGISTER_DESC + "}");

		req.setRecNum(recNum);
		req.setSmsTemplateCode(AliDayuConsts.SMS_TEMPLATE_CODE);
		AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);
			if (!rsp.isSuccess())
				logger.error("用户id{}，短信发送失败,具体错误{}", UserContextHolder.get()
						.getUserId(), rsp.getSubMsg());

		} catch (ApiException e) {
			logger.error("用户id{}，短信发送失败,具体错误{}", UserContextHolder.get()
					.getUserId(), e.getErrMsg());
		}
		return String.valueOf(code);
	}

	/**
	 * 短信发送记录查询
	 * 
	 * @param recNum
	 * @param queryDate
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	abstract AlibabaAliqinFcSmsNumQueryResponse get(String recNum,
			String queryDate, int currentPage, int pageSize);

}
